package net.hnjdzy.tinydict.Fragment;


import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.AsyncTask;
import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import net.hnjdzy.tinydict.MainActivity;
import net.hnjdzy.tinydict.R;
import net.hnjdzy.tinydict.entity.Word;
import net.hnjdzy.tinydict.service.WordManager;

import java.util.Objects;


/**
 * A simple {@link Fragment} subclass.
 */
public class WordFragment extends Fragment {


    public WordFragment() {
        // Required empty public constructor
    }


    View view;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view =  inflater.inflate(R.layout.fragment_word, container, false);
        ImageButton button=view.findViewById(R.id.imageButton);


        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EditText danci= getView().findViewById(R.id.danci);
                String word=danci.getText().toString();
                Log.d("test",word);
                TextView xianshi= getView().findViewById(R.id.English);
                xianshi.setText(word);
                ReadWordData readWordData=new ReadWordData();
                readWordData.execute(word);
            }
        });
        return view ;


    }
    class ReadWordData extends  AsyncTask<String,Void, Word>{

        @Override
        protected Word doInBackground(String... strings) {
            WordManager wordManager = new WordManager();
            try {
                Log.d("test",""+wordManager.getWordData(strings[0]));
               return wordManager.getWordData(strings[0]);
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }

        }  @Override
        protected void onPostExecute(Word wordJson) {



            EditText editText= getView().findViewById(R.id.danci);

             String input = editText.getText().toString();
             if (input.isEmpty()){
                 Toast.makeText(getContext(),"还没有输入单词",Toast.LENGTH_LONG).show();
             }else {

                 TextView ap = getView().findViewById(R.id.AmericanPronunciation);
                 ap.setText(wordJson.getSymbols().get(0).getPhAm());
                 TextView ep = getView().findViewById(R.id.BritishPronunciation);
                 ep.setText(wordJson.getSymbols().get(0).getPhEn());
                 TextView n = getView().findViewById(R.id.n);
                 n.setText("" + wordJson.getSymbols().get(0).getParts().get(0).getMeans());
                 TextView v = getView().findViewById(R.id.v);
                 v.setText("" + wordJson.getSymbols().get(0).getParts().get(1).getMeans());
                 TextView wl = getView().findViewById(R.id.NetworkLanguage);
                 wl.setText("" + wordJson.getSymbols().get(0).getParts().get(3).getMeans());
                 TextView bh = getView().findViewById(R.id.MorphologicalTransformation);
                 bh.setText("" + wordJson.getExchange().getWordDone() + wordJson.getExchange().getWordEr() + wordJson.getExchange().getWording() + wordJson.getExchange().getWordPast() + wordJson.getExchange().getWordThird());
             }
        }


}
}
